package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumPicAdapter;
import flc.ast.adapter.SelAlbumAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyCollectBean;
import flc.ast.databinding.ActivityAlbumBinding;
import flc.ast.dialog.AddPicDialog;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.basic.view.container.StkRecycleView;

public class AlbumActivity extends BaseAc<ActivityAlbumBinding> {

    public static String albumId = "";
    private MyAlbumBean currentBean;
    private List<MyCollectBean> listShow = new ArrayList<>();
    private AlbumPicAdapter picAdapter;
    private List<String> listSel = new ArrayList<>();
    private boolean isManage;
    private SelAlbumAdapter selAlbumAdapter;
    private Dialog mySelAlbumDialog;
    private Dialog myAddAlbumDialog;
    private EditText dialogAdd;
    private int oldPosition;
    private AddPicDialog addPicDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAlbumManage.setOnClickListener(this);
        mDataBinding.tvAlbumComplete.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivMove.setOnClickListener(this);
        mDataBinding.ivAddImage.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        picAdapter = new AlbumPicAdapter();
        mDataBinding.rvPictureList.setAdapter(picAdapter);
        picAdapter.setOnItemClickListener(this);

        selAlbumAdapter = new SelAlbumAdapter();
        selAlbumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAlbumManage://管理
                isManage = true;
                picAdapter.setManage(isManage);
                mDataBinding.ivAlbumManage.setVisibility(View.INVISIBLE);
                mDataBinding.tvAlbumComplete.setVisibility(View.VISIBLE);
                mDataBinding.llManageBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAlbumComplete://完成
                cancelEdit();
                break;
            case R.id.ivDelete://删除
                if (listSel.size() == 0) return;
                deleteAlbum();
                break;
            case R.id.ivCollect://收藏
                collectPicture();
                cancelEdit();
                break;
            case R.id.ivMove://移动到
                getAlbumData();
                mySelAlbumDialog.show();
                break;
            //选择相册弹框
            case R.id.ivDialogSelAlbumCancel://取消
                mySelAlbumDialog.dismiss();
                break;
            case R.id.tvDialogSelAlbumConfirm://确定
                List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
                listSp.get(oldPosition).getListData().addAll(listSel);
                AppSpUtils.saveAlbumList(listSp);
                mySelAlbumDialog.dismiss();
                deleteAlbum();
                break;
            case R.id.llDialogSelAlbumAdd://新建相册
                //新建相册弹框
                addAlbumDialog();
                myAddAlbumDialog.show();
                break;
            //新建相册弹框
            case R.id.tvDialogAddAlbumConfirm://确定
                addAlbum();
                break;
            case R.id.ivDialogAddAlbumCancel://取消
                myAddAlbumDialog.dismiss();
                break;
            case R.id.ivAddImage://添加图片
                addPicDialog();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == picAdapter) {
            if (position == 0) {
                addPicDialog();
            } else {
                if (isManage) {
                    if (picAdapter.getItem(position).isSelected()) {
                        picAdapter.getItem(position).setSelected(false);
                        listSel.remove(picAdapter.getItem(position).getPath());
                    } else {
                        listSel.add(picAdapter.getItem(position).getPath());
                        picAdapter.getItem(position).setSelected(true);
                    }
                    picAdapter.notifyItemChanged(position);
                }
            }
        } else if (adapter == selAlbumAdapter) {
            selAlbumAdapter.getItem(oldPosition).setSelected(false);
            oldPosition = position;
            selAlbumAdapter.getItem(position).setSelected(true);
            selAlbumAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initData() {
        getData();
        //选择相册弹框
        selAlbumDialog();
    }

    private void getData() {
        listShow.clear();
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    currentBean = bean;
                    mDataBinding.tvAlbumTitle.setText(bean.getName());
                    for (String path : bean.getListData()) {
                        listShow.add(new MyCollectBean(path, false));
                    }
                    break;
                }
            }
        }
        if (listShow.size() > 0) {
            listShow.add(0, new MyCollectBean("", false));
            picAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.llPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.llPictureNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除相册图片
    private void deleteAlbum() {
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (MyAlbumBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    bean.getListData().removeAll(listSel);
                    break;
                }
            }
            AppSpUtils.saveAlbumList(listSp);
        }
        cancelEdit();
    }

    //收藏图片
    private void collectPicture() {
        List<MyCollectBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            for (String s : listSel) {
                MyCollectBean bean = new MyCollectBean(s, false);
                if (!listSp.contains(bean)) {
                    listSp.add(bean);
                }
            }
            AppSpUtils.saveCollectList(listSp);
        } else {
            List<MyCollectBean> listSave = new ArrayList<>();
            for (String s : listSel) {
                listSave.add(new MyCollectBean(s, false));
            }
            AppSpUtils.saveCollectList(listSave);
        }
    }

    //取消编辑
    private void cancelEdit() {
        isManage = false;
        picAdapter.setManage(isManage);
        mDataBinding.ivAlbumManage.setVisibility(View.VISIBLE);
        mDataBinding.tvAlbumComplete.setVisibility(View.INVISIBLE);
        mDataBinding.llManageBottom.setVisibility(View.GONE);
        listSel.clear();
        getData();
    }

    private void getAlbumData() {
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.get(0).setSelected(true);
            selAlbumAdapter.setList(listSp);
        }
    }

    private void addAlbum() {
        String albumName = dialogAdd.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, new ArrayList<>(), false));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveAlbumList(listSp);
        } else {
            AppSpUtils.saveAlbumList(listSave);
        }
        getAlbumData();
    }

    private void addPicDialog() {
        if (addPicDialog == null) {
            addPicDialog = new AddPicDialog(mContext);
        }
        addPicDialog.setListener(new AddPicDialog.Listener() {
            @Override
            public void clickAdd() {
                getData();
            }
        });
        addPicDialog.show();
    }

    //选择相册弹框
    private void selAlbumDialog() {
        mySelAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_album, null);
        mySelAlbumDialog.setContentView(inflate);
        mySelAlbumDialog.setCancelable(true);
        Window dialogWindow = mySelAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogSelAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogSelAlbumCancel);
        StkLinearLayout add = inflate.findViewById(R.id.llDialogSelAlbumAdd);
        StkRecycleView recycleView = inflate.findViewById(R.id.rvSelAlbumList);
        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleView.setAdapter(selAlbumAdapter);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
        add.setOnClickListener(this);
    }

    //新建相册弹框
    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(true);
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogAddAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogAddAlbumCancel);
        TextView num = inflate.findViewById(R.id.tvDialogAddAlbumNum);
        num.setText(getString(R.string.input_name_num, 0));
        dialogAdd = inflate.findViewById(R.id.etDialogAddAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
        dialogAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                num.setText(getString(R.string.input_name_num, s.length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

}